//
//  JoTouchView.swift
//  Yogo
//
//  Created by WangLei on 15/7/29.
//  Copyright (c) 2015年 zhuxietong. All rights reserved.
//

import UIKit



@objc public protocol JoTouchKitDelegate
{
    func touch(index:NSInteger,data:NSMutableDictionary)
}


@objc public protocol JoTouchDelegate
{
    func touch(toucView:JoTouchView, index:NSInteger,data:NSMutableDictionary)
    
}


@objc open class JoTouchKit: UIView,MoAtrributeProtocol{
    open func mo_set_controller(controller: UIViewController) {
        
    }
    open func mo_change_localizable(obj: Any?) {
        
    }
    
    public var contentView = UIView()
    public var actionBt = UIButton()
    public weak var delegate:JoTouchKitDelegate?
    
 
    open func mo_model_reload() {
        
    }
    
    required public override init(frame: CGRect) {
        super.init(frame: frame)

        
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    
    
    open func addTouchView()
    {
        
        self.contentView = UIView(frame: [0, 0, self.frame.size.width, self.frame.size.height])
        self.contentView.clipsToBounds = true
        
        self.addSubview(self.contentView)

        addContentViews()
        
        
        self.actionBt = UIButton(frame: [0, 0, self.frame.size.width, self.frame.size.height])
        actionBt.tag = 4002
        
                let image1 = UIImage.image(color: UIColor.clear, frame: [0, 0, actionBt.frame.size.width, actionBt.frame.size.height])
                let image2 = UIImage.image(color: UIColor(white: 0.2, alpha: 0.3), frame: [0, 0, actionBt.frame.size.width, actionBt.frame.size.height])
        
        
                actionBt.setImage(image1, for: .normal)
                actionBt.setImage(image2, for: .highlighted)
        
        
        actionBt.addTarget(self, action: #selector(touchAction), for: .touchUpInside)
        self.addSubview(actionBt)
        
        addTopView()
    }
    
    open func addTopView() {
    }
    
    open func addContentViews()
    {
    }
    
    @objc open func touchAction()
    {
        self.delegate?.touch(index: self.tag, data: mo.model)
    }
}


extension String
{
    func float(sep:String,index:Int)->CGFloat
    {
        let paths = self.components(separatedBy:sep)
        return paths[index].cg_floatValue
    }
}


public class JoTouchView: UIView,JoTouchKitDelegate {
    
    
    
    public var true_height:CGFloat?
    public var layoutInfo:[String:Any]?
        {
        didSet{
            self.buildView()
        }
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    public var view_width = UIScreen.main.bounds.size.width
    
    public var kit_space:CGFloat = 0.5

    public var singleLine = false

    public var layouts = NSMutableDictionary()
    
    public var kits_class:JoTouchKit.Type?
    
    public weak var delegate:JoTouchDelegate?
    
    public var models:NSMutableArray = NSMutableArray()
        {
        didSet{
            self.loadItems()
        }
    }
    
    public init(width:CGFloat,kitType:JoTouchKit.Type=JoTouchKit.self,layout:[String:AnyObject]?=nil) {
        super.init(frame: CGRect.zero)
        self.view_width = width;
        self.kits_class = kitType
        self.layoutInfo = layout
    }
    
    public func buildView()
    {
        
        //        for one in self.subviews
        //        {
        //            one.removeFromSuperview()
        //        }
        
        if let info = self.layoutInfo
        {
            self.layouts = info.mutable_dictionary
            
            self.layoutViews()
            
        }
    }
    
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func loadItems()
    {
        
        var index = 0
        for one in self.models
        {
            if let a_view = self.viewWithTag(index+100)
            {
                if let kit = a_view as? JoTouchKit
                {
                    kit.mo.model = one as! NSMutableDictionary
                }
            }
            
            index += 1
        }
    }
    
    func layoutViews()
    {
        
        
        let kit_frame = self.layouts["frame","1*1"]
        let frame_info = kit_frame.components(separatedBy:"*")
        let frame_w_c = frame_info[0].cg_floatValue
        let frame_h_c = frame_info[1].cg_floatValue
        
        let edigStr = self.layouts["edige","0|0|0|0"]
        
        let scale = self.layouts[float:"size_scale",0.0]
        
        self.kit_space = self.layouts[float:"kit_space",0]
        
        var view_height = scale*view_width
        if let h = self.true_height
        {
            view_height = h
        }
        
        let items = self.layouts[obj:"kits",nil] as! NSMutableArray
        self.models = items

        
        let eT = edigStr.float(sep: "|", index: 0)
        let eL = edigStr.float(sep: "|", index: 1)
        let eB = edigStr.float(sep: "|", index: 2)
        let eR = edigStr.float(sep: "|", index: 3)
        
        let one_width = (view_width - eL - eR) / frame_w_c
        let one_height = (view_height - eT - eB) / frame_h_c
        
        
        
        var index = 0
        
        var sub_kits = [UIView]()
        
        for dict in items
        {
            
            
            if let one = dict as? NSMutableDictionary
            {
                
                if let old_view = self.viewWithTag(index)
                {
                    if old_view !== self
                    {
                        old_view.removeFromSuperview()
                    }
                }
                
                let f_str = one ["frame","0|0|0|0"]
                
                let originX = eL + (f_str.float(sep: "|", index: 0) * one_width)
                let originY = eT + (f_str.float(sep: "|", index: 1) * one_height)
                let rect:CGRect = [0, 0, 0, 0]
                let one_kit = kits_class!.init(frame: rect)
                
                let item_distance:CGFloat = self.kit_space
            
                
                one_kit.frame = [originX+item_distance/2.0, originY+item_distance/2.0, (one_width * f_str.float(sep: "|", index: 2))-item_distance/1.0, (one_height * f_str.float(sep: "|", index: 3))-item_distance/1.0]
                
//                one_kit.layer.borderColor = UIColor(white: 0.87, alpha: 0.0).cgColor
//                one_kit.layer.borderWidth = item_distance;
                one_kit.delegate = self;
                
                one_kit.tag = index+100
                
                one.setObject("\(index)", forKey: "__index" as NSCopying)
                
                self.addSubview(one_kit)
                sub_kits.append(one_kit)
                one_kit.addTouchView()
                one_kit.mo.model = one
                index += 1
            }
        }

        self.backgroundColor = UIColor(shex:layouts["back_hex",""])
        self.frame = [0, 0, self.view_width, view_height]
//        self.checkRightEdegKits(kits: sub_kits)
        
    }
    
//    func checkRightEdegKits(kits:[UIView]) {
//        let edigStr = self.layouts["edige","0|0|0|0"]
//        let eR = edigStr.float(sep: "|", index: 3)
//        if eR < 2
//        {
//            for one in kits
//            {
//                print("-=-=======---0000")
//                if (one.frame.origin.x + one.frame.size.width + self.kit_space) >= self.frame.size.width
//                {
//                    let f = one.frame
//                    print("-=-=======")
//                    one.frame = CGRect(x: f.origin.x, y: f.origin.y, width: (f.size.width + 1), height: f.size.height)
//                }
//                
//            }
//        }
//
//    }
    
    override public var intrinsicContentSize: CGSize
        {
        return self.frame.size
    }
    
    
    public func touch(index: NSInteger, data: NSMutableDictionary) {
        self.delegate?.touch(toucView: self, index: index-100, data: data)
    }
   }


